 <!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    <title>{$Think.config.site.title} - 商品编辑</title>

    <meta name="keywords" content="{$Think.config.site.keywords}">

    <link href="{$Think.config.site.resource_url}css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/plugins/iCheck/custom.css" rel="stylesheet">
    <!-- <link href="{$Think.config.site.resource_url}css/animate.min.css" rel="stylesheet"> -->
    <link href="{$Think.config.site.resource_url}css/style.min.css?v=4.0.0" rel="stylesheet">
    <link href="{$Think.config.site.common_resource_url}js/chosen/chosen.css" rel="stylesheet" type="text/css"/>

</head>
<style>
#cate1_chosen,#cate2_chosen,#cate3_chosen{display: none;}
#listtable .fa{
    cursor:pointer;
    color:#1ab394;
    margin-right:1em;
}
.chosen-select{
	width: 100%;
}
.mr{margin-right: 20px;}
.text-primary{color: #1ab394}
a:hover{color: #1ab394 !important}

.spec,.spec:hover{background-color: #DBDBDB;  border-color: #DBDBDB;color: #333; padding: 6px 20px}
.specactive,.specactive:hover{    background-color: #1ab394;border-color: #1ab394;color: #FFF;}

.addspec,.addspec:hover{background-color: #DBDBDB;  border-color: #DBDBDB;color: #333; padding: 6px 20px}
.addspecbox{display: none;}

.inp{color: #333}
.inpbox{background-color: #DBDBDB !important; border-color: #DBDBDB !important;}

.upimginput{
	margin-top: 15px;
	background-color: #1ab394; 
	border-color: #1ab394;
	border-radius: 3px;
    width: 274px;
    height: 32px;
    text-align: center;
    position: relative;
    display: block;
}
.upimginput label{
	padding: 5px 10px;
    font-size: 12px;
    line-height: 2;
    color: #fff;
}
.upimginput input{
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
    opacity: 0
}
#picture_box{
	width: 100%;
	background: #ccc;
	min-height: 400px;
	overflow: hidden;
}
#picture_box .picture{
	height: 200px;
	overflow: hidden;
	position: relative;
}
#picture_box .picture img{max-width: 100%; max-height: 100%;}

#picture_box .picture .dropbg{
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	z-index: 5;
	display: none;
	background: #ccc;
	opacity: 0.7;
}
#picture_box .picture:hover .dropbg{display: block;}
#picture_box .picture .dropbg .delpictuer{
	position: absolute;
	width: 20px;
	height: 20px;
	top: 20px;
	right: 20px;
	z-index: 10;
	display: block;
	font-size: 20px;
	color: #f00;
	cursor: pointer;
}
th .tag{font-size: 12px; color: #e66c6c; font-weight: normal;}
.ibox-title .tag{font-size: 14px; color: #e66c6c; font-weight: normal; display: inline-block; margin-left: 20px;}
</style>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
      <div class="row">
            <div class="col-sm-4">
                <div id="nestable-menu">
                    <button type="button" class="btn btn-primary btn-sm" onclick="window.history.back()"><i class="fa fa-backward"></i> 返回</button>
                </div>
            </div>
      </div>
      <form method="post" class="form-horizontal">
      <div class="row">
      		<!-- 商品信息 -->
          	<div class="col-sm-12">
                <div class="float-e-margins">
                    <div class="ibox-title">
                        <h5>商品信息</h5>
                        <input type="hidden" name="goods_id" value="{$info['goods_id']}">
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品分类：</label>
                                <div class="col-sm-2">
                                	<select id="cate1" data-next="cate2" name="category_id_1" class="chosen-select category"></select>
                                </div>
                                <div class="col-sm-2">
                                	<select id="cate2" data-next="cate3" name="category_id_2" class="chosen-select category"></select>
                                </div>
                                <div class="col-sm-2">
                                	<select id="cate3" data-next="" name="category_id_3" class="chosen-select category"></select>
                                </div>
                                <input type="hidden" id="category_id" name="category_id">
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品名称：</label>
                                <div class="col-sm-6">
                                    <input id="goods_name" name="goods_name" class="form-control" type="text" value="{$info.goods_name}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品单位：</label>
                                <div class="col-sm-6">
                                    <input id="unit" name="unit" class="form-control" type="text" value="{$info.unit}">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品品牌：</label>
                                <div class="col-sm-6">
                                    <select name="brand_id" class="chosen-select form-control chs">
                                        <option value="0">请选择</option>
                                        {volist name="brand" id="b"}
                                        <option value="{$b.brand_id}" {if($b['brand_id']==$info['brand_id'])} selected="selected" {/if}>{$b.brand_name}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>

                            <!-- <div class="form-group">
                                <label class="col-sm-2 control-label text-right">供应商：</label>
                                <div class="col-sm-6">
                                    <select  name="supplier_id" class="chosen-select chs">
                                        {volist name="supplier" id="s"}
                                        <option value="{$s.supplier_id}" {if($s['supplier_id']==$info['supplier_id'])} selected="selected" {/if}>{$s.supplier_name}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>


                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">显示供应商：</label>
                                <div class="col-sm-6">
                                    <select  name="display_supplier[]" class="chosen-select chs" multiple="multiple">
                                        {volist name="supplier" id="s"}
                                        <option value="{$s.supplier_id}" {if(in_array($s['supplier_id'],$info['display_supplier']))} selected="selected" {/if}>{$s.supplier_name}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div> -->
							
							<!-- <div class="form-group">
                                <label class="col-sm-2 control-label text-right">分类标签：</label>
                                <div class="col-sm-6">
                                    <select id="id_select" name="label_id[]" class="chosen-select chs" multiple="multiple">
                                    </select>
                                </div>
                            </div>
 -->
                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品关键词：</label>
                                <div class="col-sm-6">
                                    <input id="keywords" name="keywords" class="form-control" type="text" value="{$info.keywords}"  placeholder="以一个空格分开 例如：猪脚 脚 猪">
                                </div>
                            </div>
                            <!-- <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品简介：</label>
                                <div class="col-sm-6">
                                	<textarea name="introduction"  class="form-control" >{$info.introduction}</textarea>
                                </div>
                            </div> -->

                            <div class="form-group">
                                <label class="col-sm-2 control-label">是否上架：</label>
                                <div class="col-sm-6">
                                	<div class="btn btn-sm">
                                	<label for="ystatus">是</label>
                                    <input id="ystatus" name="status" class="form-control" {if($info['status']==1)} checked {/if}  type="radio" value="1" >
                                    <label for="nstate">否</label>
                                    <input id="nstatus" name="status" class="form-control"  type="radio" value="0"  {if($info['status']==0)} checked {/if}>
                                    </div>
                                </div>
                            </div>


                        </div>
                    </div>
                </div>
            </div>
            <!-- 商品信息 -->
            <!-- 商品类型 -->
            <div class="col-sm-12">
                <div class="float-e-margins">
                    <div class="ibox-title">
                        <h5>商品规格</h5>
                        <span class="tag">说明：1、双击商品规格可编辑；2、规格名称改为空表示删除该规格；3、规格数量：该规格的单位数量（价格/规格数量=单价）</span>
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品规格：</label>
                                <div class="col-sm-8">
                                	<div class="col-sm-12">
	                                	{volist name="spec" id="sv"}
	                                	<div class="spec btn mr {if(in_array($sv['spec_id'],$info['specidArr']))} specactive{/if} " data-specid="{$sv.spec_id}">{$sv.spec_name}</div>
	                                	{/volist}
	                                	<a class="text-primary addsepc" href="javascript:void(0);">添加规格</a>
                                	</div>
                                	<div class="col-sm-12 addspecbox">
                                		<div class="btn addspec btn-sm" >
                                			<input type="text" name="nws">
                                		</div>
                                		<div class="btn btn-primary btn-sm savespec">保存</div>
                                		<div class="btn btn-white btn-sm canclespec">取消</div>
                                	</div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">规格值：</label>
                                <div class="col-sm-8">
                                	<div class="col-sm-12" id="specvalBox">
                                		{volist name="info['specval']" id="svo"}
                                		<div class="panel panel-default" id="specbox_{$svo.spec_id}" data-specname="{$svo.spec_name}" data-specid="{$svo.spec_id}">
										<div class="panel-heading" style="padding: 4px 8px">
											<span class="btn btn-sm" specname style="margin-bottom: 0;">{$svo.spec_name}</span>
											<span class="pull-right btn btn-primary btn-sm addspecval" style="margin-bottom: 0;">添加规格值</span>
											<span class="clearfix"></span>
										</div>
										<table class="table">
											<tr>
												<td class="text-center" specval_name>规格说明</td>
												<td class="text-center" price>价格</td>
												<td class="text-center" cost_price>成本价</td>
                                                <td class="text-center" specvalnum>规格数量</td>
                                                <td class="text-center" unit>单位</td>
                                                <td class="text-center" unit_price>单价</td>
												<!-- <td class="text-center" brand>品牌</td> -->
                                                <!-- <td class="text-center" supplier>供应商</td> -->
												<td class="text-center" >操作</td>
											</tr>
											{volist name="svo['values']" id="vo"}
											<tr class='cont_tr' data-num='{$i}'>
												<td specval_name>
													<input type="text" name="specval[{$vo.spec_id.'_'.$i}][specval_name]" class="form-control" value="{$vo.specval_name}">

													<input type="hidden" name="specval[{$vo.spec_id.'_'.$i}][spec_id]" class="form-control" value="{$vo.spec_id}">

													<input type="hidden" name="specval[{$vo.spec_id.'_'.$i}][spec_name]" specname class="form-control" value="{$vo.spec_name}">

													<input type="hidden" name="specval[{$vo.spec_id.'_'.$i}][specval_id]" specname class="form-control" value="{$vo.specval_id}">
												</td>
												<td price>
													<input type="text" name="specval[{$vo.spec_id.'_'.$i}][price]" class="form-control price" value="{$vo.price}">
												</td>
												<td cost_price>
													<input type="text" name="specval[{$vo.spec_id.'_'.$i}][cost_price]" class="form-control" value="{$vo.cost_price}">
												</td>
                                                <td specvalnum>
                                                    <input type="text" name="specval[{$vo.spec_id.'_'.$i}][specvalnum]" class="form-control specvalnum" value="{$vo.specvalnum}" placeholder="用于计算单价">
                                                </td>
                                                <td unit>
                                                    <input type="text" name="specval[{$vo.spec_id.'_'.$i}][unit]" class="form-control" value="{$vo.unit}">
                                                </td>
                                                <td unit_price>
                                                    <input type="text" name="specval[{$vo.spec_id.'_'.$i}][unit_price]" class="form-control unit_price" value="{$vo.unit_price}">
                                                </td>
												<td picture>
													<span class="pull-right btn btn-sm fa fa-trash-o checkorder" data-specval_id="{$vo.specval_id}" style="margin-bottom: 0;"></span>
												</td>
											</tr>
											{/volist}
										</table>
									</div>
									{/volist}
                                	</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 商品类型 -->
            <!-- 商品图片 -->
            <div class="col-sm-12">
                <div class="float-e-margins">
                    <div class="ibox-title">
                        <h5>商品图片</h5>
                        <span class="tag">说明：第一张显示在商品列表页，拖动图片可以排序。尺寸：650*400</span>
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品图：</label>
                                <div class="col-sm-6">
                                	<div id="upimg">
                                		<div id="picture_box">
                                			{volist name="info['img_lists']" id="imgvo"}
                                			<div class="picture preview col-sm-3 draggable-element" href="{$imgvo}" title="">
                                				<img src="{$imgvo}">
                                				<input type="hidden" name="img_lists[]" value="{$imgvo}">
                                				<div class="dropbg"><span class="delpictuer fa fa-trash-o"></span></div>
                                			</div>
                                			{/volist}
                                		</div>
                                		<span class="clearfix"></span>
                                		<div class="">
                                			<span class="upimginput">
                                				<label>上传图片</label>
                                				<input type="file" name="files[]" multiple="multiple">
                                			</span>
                                		</div>
                                		
                                	</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 商品图片 -->
            <!-- 商品详情 -->
            <div class="col-sm-12">
                <div class="float-e-margins">
                    <div class="ibox-title">
                        <h5>商品详情</h5>
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                            <div class="form-group">
                                <label class="col-sm-2 control-label text-right">商品描述：</label>
                                <textarea id="description" name="description" class="col-sm-6" style="height: 500px;">{$info['description']}</textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 商品详情 -->
            <!-- 保存 -->
            <div class="col-sm-12">
                <div class="float-e-margins">
                    <div class="ibox-content">
                        <div class="row">
                            <div class="form-group">
                                <div class="col-sm-5 col-sm-offset-2">
     
                                    <input type="submit" class="btn btn-primary" value="提交">
                           
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 保存 -->
        </div>
        </form>
    </div>

    <script src="{$Think.config.site.resource_url}js/jquery.min.js?v=2.1.4"></script>
    <script src="{$Think.config.site.resource_url}js/bootstrap.min.js?v=3.3.5"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/peity/jquery.peity.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/iCheck/icheck.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/layer/layer.js"></script>
    <script src="{$Think.config.site.common_resource_url}js/majax.js"></script>
    <script src="{$Think.config.site.common_resource_url}js/jedit.js"></script>
    <script src="{$Think.config.site.resource_url}js/jquery.imagePreview.1.0.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/validate/jquery.validate.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/validate/messages_zh.min.js"></script>

    <!-- 编辑器 -->
    <script type="text/javascript" charset="utf-8" src="{$Think.config.site.common_resource_url}js/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="{$Think.config.site.common_resource_url}js/ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="{$Think.config.site.common_resource_url}js/ueditor/lang/zh-cn/zh-cn.js"></script>
    <!-- 编辑器 -->
 
    <script type="text/javascript" src="{$Think.config.site.common_resource_url}js/chosen/chosen.jquery.js"></script>

    <script type="text/javascript" src="{$Think.config.site.common_resource_url}js/dmuploader.min.js"></script>
    <script type="text/javascript" src="{$Think.config.site.common_resource_url}js/jquery-dragarrange.js"></script>

    <script type="text/html" id="specvaltable">
    	<div class="panel panel-default" id="specbox_#[spec_id]#" data-specname="#[spec_name]#" data-specid="#[spec_id]#">
		<div class="panel-heading" style="padding: 4px 8px">
			<span class="btn btn-sm" specname style="margin-bottom: 0;">#[spec_name]#</span>
			<span class="pull-right btn btn-primary btn-sm addspecval" style="margin-bottom: 0;">添加规格值</span>
			<span class="clearfix"></span>
		</div>
		<table class="table">
			<tr>
				<td class="text-center" specval_name>规格说明</td>
				<td class="text-center" price>价格</td>
				<td class="text-center" cost_price>成本价</td>
                <td class="text-center" specvalnum>规格数量</td>
                <td class="text-center" unit>单位</td>
                <td class="text-center" unit_price>单价</td>
				<!-- <td class="text-center" brand>品牌</td> -->
                <!-- <td class="text-center" supplier>供应商</td> -->
				<td class="text-center" >操作</td>
			</tr>
			<tr class='cont_tr' data-num='#[num]#'>
				<td specval_name>
					<input type="text" name="specval[#[sign]#][specval_name]" class="form-control">
					<input type="hidden" name="specval[#[sign]#][spec_id]" class="form-control" value="#[spec_id]#">
					<input type="hidden" name="specval[#[sign]#][spec_name]" specname class="form-control" value="#[spec_name]#">
				</td>
				<td price>
					<input type="text" name="specval[#[sign]#][price]" class="form-control price" value="0">
				</td>
				<td cost_price>
					<input type="text" name="specval[#[sign]#][cost_price]" class="form-control" value="0">
				</td>
                <td specvalnum>
                    <input type="text" name="specval[#[sign]#][specvalnum]" class="form-control specvalnum" value="" placeholder="用于计算单价">
                </td>
                <td unit>
                    <input type="text" name="specval[#[sign]#][unit]" class="form-control" value="#[spec_unit]#">
                </td>
                <td unit_price>
                    <input type="text" name="specval[#[sign]#][unit_price]" class="form-control unit_price" value="0">
                </td>
				<!-- <td brand>
                    <select name="specval[#[sign]#][brand_id]" class="chosen-select form-control">
                        <option value="0">请选择</option>
                        {volist name="brand" id="b"}
                        <option value="{$b.brand_id}">{$b.brand_name}</option>
                        {/volist}
                    </select>
                </td> -->
                <!-- <td supplier>
                    <select  name="specval[#[sign]#][supplier_id]" class="chosen-select form-control">
                        <option value="0">请选择</option>
                        {volist name="supplier" id="s"}
                        <option value="{$s.supplier_id}">{$s.supplier_name}</option>
                        {/volist}
                    </select>
                </td> -->
				<td picture>
					<span class="pull-right btn btn-sm fa fa-trash-o" style="margin-bottom: 0;"></span>
				</td>
			</tr>
		</table>
	</div>
    </script>

    <script type="text/html" id="specvaltr">
    	<tr class='cont_tr'  data-num='#[num]#'>
			<td specval_name>
				<input type="text" name="specval[#[sign]#][specval_name]" class="form-control">
				<input type="hidden" name="specval[#[sign]#][spec_id]" class="form-control" value="#[spec_id]#">
				<input type="hidden" name="specval[#[sign]#][spec_name]" class="form-control" specname value="#[spec_name]#">
			</td>
			<td price>
				<input type="text" name="specval[#[sign]#][price]" class="form-control price" value="0">
			</td>
			<td cost_price>
				<input type="text" name="specval[#[sign]#][cost_price]" class="form-control" value="0">
			</td>
            <td specvalnum>
                <input type="text" name="specval[#[sign]#][specvalnum]" class="form-control specvalnum" value="" placeholder="用于计算单价">
            </td>
            <td unit>
                <input type="text" name="specval[#[sign]#][unit]" class="form-control" value="#[spec_unit]#">
            </td>
            <td unit_price>
                <input type="text" name="specval[#[sign]#][unit_price]" class="form-control unit_price" value="0">
            </td>
			<td picture>
				<span class="pull-right btn btn-sm fa fa-trash-o" style="margin-bottom: 0;"></span>
			</td>
		</tr>
	</div>
    </script>

    <script type="text/javascript">
    	var catelist = {$catelist};
    	var ue = UE.getEditor('description',{
    		serverUrl:"{:url('ueditor/index')}",
            toolbars: [[
                'fullscreen', 'source', '|', 'undo', 'redo', '|',
                'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
                'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
                'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
                'directionalityltr', 'directionalityrtl', 'indent', '|',
                'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
                'simpleupload', 'insertimage', 'emotion', 'scrawl', 'pagebreak', 'template', 'background', '|',
                'horizontal'
            ]]
    	});

    	var timer = null;
    	$('.spec').jedit({
    		class:'inpbox',
    		inputclass:'inp',
    		before:function(old){
    			clearTimeout(timer);
    		},
    		complete:function(n){

    		},
    		cancle:function(n){

    		},
    		change:function(n,old){
    			var self = this;
    			majax('{:url("editSpec")}',{spec_name:n,spec_id:$(self).data('specid')},function(d){
    				if (d.code>0) {
                        if (d.data.spec_name=='') {
                            $('#specbox_'+$(self).data('specid')).remove();
                            $(self).remove();
                        }else{
                            $('#specbox_'+$(self).data('specid')).find('span[specname]').text(n);
                            $('#specbox_'+$(self).data('specid')).find('input[specname]').val(n);
                        }
                    }else{
                        $(self).text(old);
                        layer.msg(d.msg, {icon: 5});
                    }
    			})
    		}
    	});
    	$('.spec').click(function(){
    		var self = this;
    		clearTimeout(timer);
    		timer = setTimeout(function() { //在单击事件中添加一个setTimeout()函数，设置单击事件触发的时间间隔 
                $(self).toggleClass('specactive');
	    		var spec_id = $(self).data('specid'),spec_name=$(self).text(),num = 0;
	    		if ($(self).hasClass('specactive')) {
	    			var html = $('#specvaltable').html();
	    			var reg = new RegExp("#\\[([^\\[\\]]*?)\\]#", 'igm'); 
	    			var source = html.replace(reg, function(node, key) {  
				      return {
				        'spec_id' : spec_id,
				        'spec_name' : spec_name,
				        'spec_unit' : $('[name="unit"]').val(),
				        'num' : num,
				        'sign': spec_id+'_'+num,
				      }[key];  
				    });
                    var so=$(source);
                    // $("select",so).chosen({no_results_text: "没有找到：",width:'130px'});
                    // $("select",so).trigger("chosen:updated");
                    $('#specvalBox').append(so);
	    		}else{
                    var list = [];
                    $('#specbox_'+spec_id).find('.checkorder').each(function(){
                        list.push($(this).data('specval_id'))
                    });
                    if (list.length>0) {
                        // console.log(list.join(','));
                        majax('{:url("checkorder")}',{ids:list.join(',')},function(re){
                            if (re.code==0) {
                                layer.msg(re.msg,{icon:2});
                                $(self).toggleClass('specactive');
                            }else{
                                $('#specbox_'+spec_id).remove();
                            }
                        });
                    }else{
                        $('#specbox_'+spec_id).remove();
                    }
	    		}
            }, 300);

    		
    	})
    	
        $('#specvalBox').delegate('.specvalnum,.price','change',function(){
            var tr=$(this).closest('tr'), num = parseInt(tr.find('input.specvalnum').val()), price = parseFloat(tr.find('input.price').val()),
            unit_price = 0;
            var re = /^[0-9]+.?[0-9]*$/;
            if (re.test(price/num)) {
                unit_price = price/num;
            }
            tr.find('input.unit_price').val(unit_price.toFixed(2));
        })

    	$('#specvalBox').delegate('.addspecval','click',function(){

    		var p=$(this).closest('.panel'),spec_id = p.data('specid'),spec_name=p.data('specname'),ptr = p.find('.cont_tr:last'),num = ptr.data('num')+1;
    		var html = $('#specvaltr').html();
			var reg = new RegExp("#\\[([^\\[\\]]*?)\\]#", 'igm'); 
			var source = html.replace(reg, function(node, key) {  
		      return {
		        'spec_id' : spec_id,
		        'spec_name' : spec_name,
		        'spec_unit' : $('[name="unit"]').val(),
		        'num' : num,
			    'sign': spec_id+'_'+num,
		      }[key];  
		    });
            var so=$(source);
            // ptr.find('select').each(function(i,e){
            //     so.find('select:eq('+i+')').find('option[value="'+$(e).val()+'"]').attr('selected','selected');
            // })
            // $("select",so).chosen({no_results_text: "没有找到：",width:'130px'});
            // $("select",so).trigger("chosen:updated");

            p.find('table').append(so);
    	})

    	$('#specvalBox').delegate('.fa-trash-o','click',function(){
             var self = this,specval_id=$(self).data('specval_id');
            layer.confirm('删除该规格？', {
                btn: ['确定','取消'], //按钮
                shade: false //不显示遮罩
            }, function(){
               
                if ($(self).hasClass('checkorder')) {
                    majax('{:url("checkorder")}',{ids:specval_id},function(re){
                        if (re.code==0) {
                            layer.msg(re.msg,{icon:2});
                        }else{
                            $(self).closest('tr').remove();
                        }
                    });
                }else{
                    $(self).closest('tr').remove();
                }
            },
            function(){
                
            });
            
    		
    	})

    	$('.canclespec').click(function(){
    		$('.addsepc').show();
    		$('.addspecbox').hide();
    		$('.addspecbox input').val('');
    	})

    	$('.addsepc').click(function(){
    		$(this).hide();
    		$('.addspecbox').show();
    		$('.addspecbox input').focus();
    	})

    	$('.savespec').click(function(){
    		var spec_name = $.trim($('.addspecbox input').val());
    		if (spec_name!='') {
    			var self = this;
    			majax('{:url("editSpec")}',{spec_name:spec_name},function(d){
    				if (d.code>0) {
    					var str = '<div class="spec btn mr " data-specid="'+d.data['spec_id']+'">'+d.data['spec_name']+'</div>';
    					$('.canclespec').click();
    					$('.addsepc').before(str);
    				}else{
    					layer.msg(d.msg, {icon: 5});
    				}
    			})
    		}else{
    			$('.canclespec').click();
    		}
    	})
    	var ojb_cate={cate1:{$info['category_id_1']},cate2:{$info['category_id_2']},cate3:{$info['category_id_3']}}
    	function initCate(id,pid) {
    		if (id=='') {return 0;}
    		
    		$('#'+id+' option').remove();
    		var val = $('#'+id).val(),num=0;

    		var option = '<option value="-1">请选择分类</option>';
    		for (var i = 0; i < catelist.length; i++) {
    			if (catelist[i].cate_pid==pid) {
    				var selected='';
    				if (ojb_cate[id]>0 && ojb_cate[id]==catelist[i].cate_id) {
    					selected='selected';ojb_cate[id]=0;
    				}
    				num++;
    				option += '<option value="'+catelist[i].cate_id+'" '+selected+'>'+catelist[i].cate_name+'</option>';
    			}
    		}

    		if (num<=0) {
				$("#"+id+'_chosen').hide();
			}else{
				$('#'+id).append(option);
	    		$(".category").chosen({no_results_text: "没有找到："});
	    		$("#"+id).trigger("chosen:updated");
				$("#"+id+'_chosen').show();
			}
			if (pid == 0) {
				$('#'+id).change();
			}else{
				return num;
			}
			
    	}

    	$('.category').change(function(){
    		var val = $(this).val(),next=$(this).data('next');
    		var num = initCate(next,val);
    		if (num<=0) {
    			if (val!=null) {
					$('#category_id').val(val);
					$('#category_id').change();
				}
    		}else{
    			$('#category_id').val(-1);
    		}
    		if (next!='') {
    			$('#'+next).change();
    		}
    	})
		
		// $('#category_id').change(function(){
  //           var category_id = $(this).val();
  //           if(category_id > 0){
		// 		var label_id = {$info['label_id']};
  //               majax('{:url("label")}',{category_id : category_id},function(d){
  //                   if(d.code > 0){
  //                       for(var i=0;i<d.data.length;i++){
  //                           var option='<option value="'+d.data[i]['label_id'];
		// 					if($.inArray(parseInt(d.data[i]['label_id']),label_id)>-1){
		// 						option+= '" selected="selected" >'+d.data[i]['label_name']+"</option>";
		// 					}else{
		// 						option+= '">'+d.data[i]['label_name']+"</option>";
		// 					}
  //                           $("#id_select").append(option);
  //                       }
  //                       $("#id_select").chosen({no_results_text: "没有找到："});
  //                       $("#id_select").trigger("chosen:updated");
  //                   }else{
  //                       $("#id_select option").remove();
  //                       $("#id_select").trigger("chosen:updated");
  //                   } 
  //               });
  //           }else{
  //               $("#id_select option").remove();
  //               $("#id_select").trigger("chosen:updated");
  //           }
  //       })

    	$('#upimg').dmUploader({
	        url: '{:url("upload/index")}',
	        dataType: 'json',
	        allowedTypes: 'image/*',
	        /*extFilter: 'jpg;png;gif',*/
	        onInit: function(){
	        },
	        onBeforeUpload: function(id){
	         
	        },
	        onNewFile: function(id, file){
	        },
	        onComplete: function(){
	        	$('.draggable-element').arrangeable();
	        },
	        onUploadProgress: function(id, percent){
	        },
	        onUploadSuccess: function(id, d){

	        	if (d.code>0) {
	        		var str = '<div class="picture preview col-sm-3 draggable-element" href="'+d.data+'" title="">\
	            				<img src="'+d.data+'">\
	            				<input type="hidden" name="img_lists[]" value="'+d.data+'">\
	            				<div class="dropbg"><span class="delpictuer fa fa-trash-o"></span></div>\
	            			</div>';
                    $('#picture_box').append(str);
                    $('.preview').preview();
	            }else{
	            	layer.msg(d.msg, {icon: 5});
	            }

	        },
	        onUploadError: function(id, message){
	        },
	        onFileTypeError: function(file){
	          
	        },
	        onFileSizeError: function(file){
	        },
	        /*onFileExtError: function(file){
	          $.danidemo.addLog('#demo-debug', 'error', 'File \'' + file.name + '\' has a Not Allowed Extension');
	        },*/
	        onFallbackMode: function(message){
	        }
	      });

    	$('body').delegate('.delpictuer','click',function(){
    		$(this).closest('.picture').remove();
    	})

    	function save(){
			var fomrData = $("form").serialize();

			majax('{:url("editGoods")}',fomrData,function(d){
				if (d.code>0) {
					layer.msg(d.msg, {icon: 1});
				}else{
					layer.msg(d.msg, {icon: 5});
				}
			})
		};

		$(function() {
    		initCate('cate1',0);
            $(".chs").chosen({no_results_text: "没有找到："});
            $("select").chosen({no_results_text: "没有找到：",width:'130px'});
    		$("input").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});
    		$('.draggable-element').arrangeable();
    		$("form").validate({
			  debug:false,
			  submitHandler:function(form){
		            save();   
		      },    
			  rules: {
				goods_name: {
		            required: true,
				},
				
			  },
			  messages: {
				goods_name: {
					required: '商品名必须填写',
				},
			  }
			});
            $('.preview').preview();
    	});
    </script>
</body>

</html>
